package web_programing.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author haomin
 * @date 2022/10/11 22:15
 **/
public class HttpClient {
    public HttpClient(){}

    public static void main(String[] args) throws Exception{
        Socket socket = new Socket("127.0.0.1", 80);
        OutputStream os = socket.getOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "utf-8"));
        String request = "GET / HTTP/1.0\r\n\r\n";
        writer.println(request);
        writer.flush();

        InputStream is = socket.getInputStream();
        byte[] buf = new byte[10240];
        int n = is.read(buf);
        String response = new String(buf, 0, n, "utf-8");
        System.out.println(response);
    }
}